package cn.iocoder.yudao.module.erp.controller.admin.purchasemanage;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.purchasemanage.vo.PurchaseManagePageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchasemanage.vo.PurchaseManageRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchasemanage.vo.PurchaseManageSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchasemanage.PurchaseManageDO;
import cn.iocoder.yudao.module.erp.service.purchasemanage.PurchaseManageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 采购管理主")
@RestController
@RequestMapping("/erp/purchase-manage")
@Validated
public class PurchaseManageController {

    @Resource
    private PurchaseManageService purchaseManageService;

    @PostMapping("/create")
    @Operation(summary = "创建采购管理主")
    @PreAuthorize("@ss.hasPermission('erp:purchase-manage:create')")
    public CommonResult<Long> createPurchaseManage(@Valid @RequestBody PurchaseManageSaveReqVO createReqVO) {
        return success(purchaseManageService.createPurchaseManage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新采购管理主")
    @PreAuthorize("@ss.hasPermission('erp:purchase-manage:update')")
    public CommonResult<Boolean> updatePurchaseManage(@Valid @RequestBody PurchaseManageSaveReqVO updateReqVO) {
        purchaseManageService.updatePurchaseManage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除采购管理主")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:purchase-manage:delete')")
    public CommonResult<Boolean> deletePurchaseManage(@RequestParam("id") Long id) {
        purchaseManageService.deletePurchaseManage(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除采购管理主")
                @PreAuthorize("@ss.hasPermission('erp:purchase-manage:delete')")
    public CommonResult<Boolean> deletePurchaseManageList(@RequestParam("ids") List<Long> ids) {
        purchaseManageService.deletePurchaseManageListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得采购管理主")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:purchase-manage:query')")
    public CommonResult<PurchaseManageRespVO> getPurchaseManage(@RequestParam("id") Long id) {
        PurchaseManageDO purchaseManage = purchaseManageService.getPurchaseManage(id);
        return success(BeanUtils.toBean(purchaseManage, PurchaseManageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得采购管理主分页")
    @PreAuthorize("@ss.hasPermission('erp:purchase-manage:query')")
    public CommonResult<PageResult<PurchaseManageRespVO>> getPurchaseManagePage(@Valid PurchaseManagePageReqVO pageReqVO) {
        PageResult<PurchaseManageDO> pageResult = purchaseManageService.getPurchaseManagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PurchaseManageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出采购管理主 Excel")
    @PreAuthorize("@ss.hasPermission('erp:purchase-manage:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPurchaseManageExcel(@Valid PurchaseManagePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PurchaseManageDO> list = purchaseManageService.getPurchaseManagePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "采购管理主.xls", "数据", PurchaseManageRespVO.class,
                        BeanUtils.toBean(list, PurchaseManageRespVO.class));
    }

}